<?php

if (!is_object(cmsms())) exit;

// don't trust that this was safe, just because the link is hidden if the option is off!
// always code defensively! There are beasties that lurk and things go bump in the night.
#if (! $this->GetPreference('allow_add',1) == 1) exit;



// get our records from the database
$db = $gCms->GetDb();

if (isset($params['cntr_id']))
{
   $query = 'SELECT * from ' . cms_db_prefix().
      'module_counters where cntr_id = ?';
   $result = $db->Execute($query,array($params['cntr_id']));

   if ($result !== false)
   {
        $row = $result->FetchRow();
        
        $cntr = new stdClass();
        $cntr->id = $row['cntr_id'];     
        $cntr->name = $row['name'];     
        $cntr->description = $row['description'];     
        $cntr->explanation = html_entity_decode($row['explanation']);     
        $cntr->value = $row['value'];     
        $cntr->ceiling = $row['ceiling'];     
        $cntr->product_id = $row['product_id'];
        $sid = $cntr->id;     
       
   }
   else
   {
        // put mention into the admin log
        $this->Audit( $params['cntr_id'], 
              $this->Lang('friendlyname'), 'DB Error on admin addedit action.');
          
      // yeah, that's graceful :(
      echo "Database error!";
      return;
   }
}
else
{
   // if we didn't retrieve a record, set some default values
   $sid = -1;
   $desc = '';
   $exp = '';
}

// set up form for Smarty
$smarty->assign('start_form', $this->CreateFormStart($id, 'admin_save_record', $returnid));

// give Smarty translated field titles 
$smarty->assign('title_name',$this->Lang('title_name'));
$smarty->assign('title_description',$this->Lang('title_description'));
$smarty->assign('title_explanation',$this->Lang('title_explanation'));
$smarty->assign('title_value',$this->Lang('title_value'));
$smarty->assign('title_ceiling',$this->Lang('title_ceiling'));
$smarty->assign('title_product_id',$this->Lang('title_product_id'));
#$smarty->assign('title_explanation',$this->Lang('title_explanation'));


// create inputs for the Form elements, and pass them to Smarty. You'd best look up the crazy long parameter
// lists for the Form API in lib/classes/class.module.inc.php
$smarty->assign('input_name',$this->CreateInputText($id, 'name', $cntr->name));
$smarty->assign('input_description',$this->CreateInputText($id, 'description', $cntr->description));
$smarty->assign('input_ceiling',$this->CreateInputText($id, 'ceiling', $cntr->ceiling));
$smarty->assign('input_explanation',$this->CreateTextArea(true, $id, $cntr->explanation, 'explanation', '', '', '', '', 40, 5));
$smarty->assign('input_product_id',$this->CreateInputText($id,'product_id',$cntr->product_id));

// pass a hidden key value along with the submit button
$smarty->assign('submit', $this->CreateInputHidden($id,'cntr_id',$sid).$this->CreateInputSubmit($id, 'submit', $this->Lang('submit')));
$smarty->assign('end_form', $this->CreateFormEnd());


// Display the populated template
echo $this->ProcessTemplate('add_edit.tpl');   

?>